"""
    This file is part of django-simp.

    django-simp is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    django-simp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with django-simp.  If not, see <http://www.gnu.org/licenses/>.
"""

import os.path, shutil, os
from django.db import models
from django.utils import simplejson
from django.contrib import admin
from PIL import Image

from simpdemo.settings import *

import uuid

class DemoImageManager(models.Manager):
    pass

class DemoImage(models.Model):
    title = models.CharField(max_length=128)
    desc = models.CharField(max_length=1028)
    image = models.ImageField(upload_to="simpdemo/")

    objects = DemoImageManager()

    def __unicode__(self):
        return "%s: %s" % (self.title, self.image)

    def __str__(self):
        return "%s: %s" % (self.title, self.image)
    
    def get_image_thumb(self):
        if os.path.exists(self.get_image_filename()): 
            width = float(self.get_image_width())
            height = float(self.get_image_height())
            factor = float(200)/width
            if width < height:
                factor = float(200)/height
            sizeX = width * factor
            sizeY = height * factor
            size = (sizeX, sizeY)
            #raise "%s:%s" % (sizeX, sizeY);
            # make new thumb
            imgobj = Image.open(self.get_image_filename())  
            thumb = imgobj.resize(size, Image.ANTIALIAS) # best downsize filter
            thumb.save(self.get_image_filename().replace(".jpg", ".thumb.jpg"))     
        return self.get_image_url().replace(".jpg", ".thumb.jpg")

    class Meta:
        ordering = ['id', 'title']
        
class DemoImageOptions(admin.ModelAdmin):
    list_display = ('title', 'desc', 'image')        
        
admin.site.register(DemoImage, DemoImageOptions)

class GalleryImageManager(models.Manager):
    pass

class GalleryImage(models.Model):
    demoImage = models.ForeignKey(DemoImage, related_name="galleryImages")
    desc = models.CharField(max_length=1028)
    created = models.DateField(auto_now=True)
    image = models.ImageField(upload_to="simpdemo/gallery/")
    
    def __unicode__(self):
        return "%s:%s" % (self.demoImage, self.id)
    
    def __str__(self):
        return "%s:%s" % (self.demoImage, self.id)
    
    def get_image_thumb(self):
        if os.path.exists(self.get_image_filename()): 
            width = float(self.get_image_width())
            height = float(self.get_image_height())
            factor = float(200)/width
            if width < height:
                factor = float(200)/height
            sizeX = width * factor
            sizeY = height * factor
            size = (sizeX, sizeY)
            #raise "%s:%s" % (sizeX, sizeY);
            # make new thumb
            imgobj = Image.open(self.get_image_filename())  
            thumb = imgobj.resize(size, Image.ANTIALIAS) # best downsize filter
            thumb.save(self.get_image_filename().replace(".jpg", ".thumb.jpg"))     
        return self.get_image_url().replace(".jpg", ".thumb.jpg")

    class Meta:
        ordering = ['created']
        
class GalleryImageOptions(admin.ModelAdmin):
    list_display = ('id', 'demoImage', 'desc', 'created', 'image')   

admin.site.register(GalleryImage, GalleryImageOptions)



